package com.lifan.pan.openapi.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lifan.pan.openapi.entity.AccessToken;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class OpenApiService {
    private static String Domain = "https://open-api.123pan.com";
    // 获取accessToken
    public String GetAccessToken(String clientID, String clientSecret) {
        // 发起http请求
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("clientID", clientID);
        requestMap.put("clientSecret", clientSecret);
        String requestBody = JSONUtil.toJsonStr(requestMap);
        String result = HttpRequest.post(Domain + "/api/v1/access_token").
                header("Platform", "open_platform").
                body(requestBody).execute().body();


        try {
            ObjectMapper obj = new ObjectMapper();
            AccessToken accessToken = obj.readValue(result, AccessToken.class);
            System.out.println(accessToken.getAccessToken());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
